(function(cdt) {
	cdt.jQ.fn.page = function(options){
		var $page = this, _o = {}
		
		_o = cdt.jQ.extend({
			
		},options)
		
		function _initialize(){
			cdt.util.log('page._initialize','info')
			_setup_events()
			$page.colorRotator({sourceSelector:".rotatedColor, .rotatedColors li, .wp-tag-cloud li"})
			$page.find("form.search").searchForm();
			$page.find(".action-button.sharing").spreadTheWord();
			return $page
		}
		
		function _setup_events(){
			
		}
		
		return _initialize()
	}
	
	cdt.jQ.fn.spreadTheWord = function(options) {
		var $spw = this, _o = {}, _active = false, $label = null, $icons = null
		
		_o = cdt.jQ.extend({
			activeClass:"reveal",
			fadeSpeed:300
		},options)
		
		function _initialize(){
			cdt.util.log('spreadTheWord._initialize','info')
			$label = $spw.find("a:first");
			$icons = $spw.find("ul");
			$icons.css('opacity',0.0)
			_setup_events()
			return $spw
		}
		
		function _setup_events(){
			$spw.bind("mousedown", _spreadTheWordInHandler);
			$spw.bind("mouseleave", _spreadTheWordOutHandler);
		}
		
		function _spreadTheWordInHandler(e) {
			if(!_active) {
				$label.stop().animate({"opacity":0.0}, _o.fadeSpeed, function() { 	
					$label.css("visibility","hidden");
				});
				$icons.stop().show().animate({"opacity":1.0}, _o.fadeSpeed);
				
				_active = true;
				$spw.removeClass(_o.activeClass);
			}
		}
		
		function _spreadTheWordOutHandler(e) {
			if(_active) {
				$label.stop().css("visibility","visible").animate({"opacity":1.0}, _o.fadeSpeed);
				$icons.stop().animate({"opacity":0.0}, _o.fadeSpeed, function() { 				
					$icons.hide();
				});

				_active = false;
				$spw.addClass(_o.activeClass);
			}
		}
						
		return _initialize()
	}
	
	cdt.jQ.fn.searchForm = function(options) {
		var $searchForm = this, _o = {}
		var $input;
		
		_o = cdt.jQ.extend({
			inactiveClass:"inactive",
			defaultVal:"Search Projects or Designers"
		},options)
		
		function _initialize(){
			cdt.util.log('searchForm._initialize','info')
			$input = $searchForm.find("input[type='text']");
			
			_checkInput();
			
			_setup_events()
			
			$input.autocomplete({source:cdt.instances.autoCompleteArray,delay:0,select:_searchFormAutocompleteSelectHandler})
			
			return $searchForm
		}
		
		function _setup_events(){
			$searchForm.find("span.button-wrap").bind("click", _searchFormButtonClickHandler)
			$input.bind("focus", _searchFormInputFocusHandler);
			$input.bind("blur", _searchFormInputBlurHandler);
		}
		
		function _searchFormButtonClickHandler(e){
			$searchForm.submit();
		}
		
		function _searchFormAutocompleteSelectHandler(e,d){
			$input.attr('value',d.item.value)
			$searchForm.submit();
		}
		
		function _searchFormInputFocusHandler(e) {
			_checkInput();
		}
		
		function _searchFormInputBlurHandler(e) {
			_checkInput();
		}
		
		function _checkInput() {
			if(!_checkEmptyInput()) {
				_checkPopulatedInput();
			}
		}
			
		function _checkEmptyInput() {
			if(cdt.util.removeSpaces($input.val()) === "") {
				$input.addClass(_o.inactiveClass);
				$input.val(_o.defaultVal);
				return true;
			} else {
				return false;
			}
		}
		
		function _checkPopulatedInput() {
			$input.removeClass(_o.inactiveClass);
			if($input.val() === _o.defaultVal) {
				$input.val("");
			}
		}
		
		return _initialize()
	}
	
	cdt.jQ.fn.contactMap = function(options) {
		var $contactMap = this, _o = {}, _map = null, _marker = null;
		
		_o = cdt.jQ.extend({
			id:'contactMap',
			lat: 40.735278,
			lng: -73.994467,
			markerHtml:"<div class='markerInfoWindow'>"+
				"<h3>Address:</h3>"+
				"<p>Sheila C. Johnson Design Center</p>"+
				"<p>66 5th Avenue at 13th Street</p>"+
				"<p>New York, New York 10011</p>"+
				"<p><a target='_blank' href='http://maps.google.com/maps?hl=en&resnum=0&q=66+5th+Avenue+at+13th+Street+New+York,+New+York+10011&um=1&ie=UTF-8&hq=&hnear=66+5th+Ave,+New+York,+NY+10011&gl=us&daddr=66%205th%20Ave,%20New%20York,%20NY%2010011&ei=lgv0S4-cGoOC8gbjqY3zDQ&sa=X&oi=geocode_result&ct=directions-to&resnum=1'>Get Directions</a></p>"+
			"</div>"
		},options)
		
		function _initialize(){
			cdt.util.log('contactMap._initialize','info')
			$contactMap.attr('id',_o.id)
			if(!cdt.instances.google_maps_loaded){
				cdt.events.one('googleMapsLoaded',_googleMapsLoadedEventHandler)
			} else {
				_buildMap()
			}
			
			return $contactMap
		}
		
		function _googleMapsLoadedEventHandler(){
			_buildMap()
		}
		
		function _buildMap(){
			cdt.util.log('contactMap._buildMap')
			if(!_map){
				_map = new google.maps.Map2(document.getElementById(_o.id))
				_map.setCenter(new google.maps.LatLng(_o.lat+0.0015, _o.lng+0.00075), 16) //adjust for the size of the infowindow
				var _myui = _map.getDefaultUI()
				_myui.maptypes = false
				_map.setUI(_myui)
			}
			_buildMarker()
		}
		
		function _buildMarker(){
			cdt.util.log('contactMap._buildMarker')
			if(!_marker){
				_marker = new google.maps.Marker(new google.maps.LatLng(_o.lat, _o.lng))
				_map.addOverlay(_marker)
				_marker.openInfoWindowHtml(_o.markerHtml)
			}
		}
		
		
		return _initialize()
	}
})(cdt);